package com.liang.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.liang.exception.CaptchaEmptyException;
import com.liang.exception.CaptchaErrorException;
import com.liang.pojo.Root;
import com.liang.service.RootService;
import com.liang.shiro.CaptchaFormAuthenticationFilter;
import com.liang.shiro.CaptchaToken;

@Controller
public class RootShiroController {
	@Autowired
	RootService rootService;
	@RequestMapping("/main")
	public String main() {
		return "main";
	}
	@RequestMapping("/toLogin")
	public String toLogin() {
		return "toLogin";
	}
	@RequestMapping("/toLogin2")
	public String toLogin2() {
		return "toLogin2";
	}
	@RequestMapping("/love")
	public String love() {
		return "love";
	}
	
	@RequestMapping("/login")
	public String login(Root root,Model model) {
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.getSession().setAttribute("info", root.getUsername());
		UsernamePasswordToken token = new UsernamePasswordToken(root.getUsername(),root.getPassword());
		if(!currentUser.isAuthenticated()) {
			try {
				currentUser.login(token);
				return "main";
			}catch (UnknownAccountException uae) {
				model.addAttribute("msg", "用户名不存在");
				return "toLogin";
	        } catch (IncorrectCredentialsException ice) {
	        	model.addAttribute("msg2", "密码错误");
				return "toLogin";
	        } catch (LockedAccountException lae) {
	        	model.addAttribute("msg", "拒绝访问");
				return "toLogin";
	        }
	        catch (AuthenticationException ae) {
	        	model.addAttribute("msg", "未知错误");
	        	return "toLogin";        
	        }
		}
		return "main";
	}
	
	@Autowired
	CaptchaFormAuthenticationFilter captchaFormAuthenticationFilter;
	@RequestMapping("/login2")
	public String login2(Root root,String captchacode,Model model) {
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.getSession().setAttribute("info", root.getUsername());
//		UsernamePasswordToken token = new UsernamePasswordToken(root.getUsername(),root.getPassword());
		CaptchaToken token=new CaptchaToken(root.getUsername(),root.getPassword(),captchacode);
		if(!currentUser.isAuthenticated()) {
			try {
				currentUser.login(token);
				return "main";
			}catch (UnknownAccountException uae) {
				model.addAttribute("msg", "用户名不存在");
				return "toLogin2";
			} catch (IncorrectCredentialsException ice) {
				model.addAttribute("msg2", "密码错误");
				return "toLogin2";
			} catch (LockedAccountException lae) {
				model.addAttribute("msg", "拒绝访问");
				return "toLogin2";
			} catch (CaptchaEmptyException emp) {
				model.addAttribute("msg3", "验证码为空");
				return "toLogin2";
			} catch (CaptchaErrorException err) {
				model.addAttribute("msg3", "验证码错误");
				return "toLogin2";
			} catch (AuthenticationException ae) {
				model.addAttribute("msg", "未知错误");
				return "toLogin2";        
			}
		}
		return "main";
	}
}
